package com.db.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.db.pojo.Odreceipt;
import com.db.pojo.StoreOdreceipt;
import com.db.service.OdreceiptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Odreceipt")
public class OdreceiptController {

    @Autowired
    private OdreceiptService odreceiptService;

    @Autowired
    private RestTemplate restTemplate;//提供多种便捷访问远程http服务的方法，简单的restful服务模板

    private final String REST_URL_PREFIX = "http://127.0.0.1:8081";

    //testhttps
    @ResponseBody
    @RequestMapping("/https")
    public String https(){
        return "https! ok!";
    }

    //前往处置交接单增加页面
    @RequestMapping("/addOdreceiptView")
    public String addOdreceiptView(){
        return "odreceipt/odreceipt";
    }

    //前往处置计划单增加页面
    @RequestMapping("/addStoreOdreceiptView")
    public String addStoreOdreceiptView(){
        return "odreceipt/storeodreceipt";
    }

    //前往处置计划单增加页面_每个商家一个码
    @RequestMapping("/addStoreOdreceiptView/{name}")
    public String addStoreOdreceiptViewSingle(@PathVariable("name") String name, Model model){
        model.addAttribute("name", name);
        return "odreceipt/storeodreceipt_single";
    }

    //处置交接单增加
    @RequestMapping("/addOdreceipt")
    @ResponseBody
    public String addOdreceipt(Odreceipt odreceipt){

        int res = odreceiptService.insertOdreceipt(odreceipt);
        JSONObject resjson = new JSONObject();
        if(res==1){
            resjson.put("odreceiptId",odreceipt.getId());
        }
        else{
            resjson.put("odreceiptId",0);
        }
        return JSON.toJSONString(resjson);
    }

    //处置计划单增加
    @RequestMapping("/addStoreOdreceipt")
    @ResponseBody
    public String addStoreOdreceipt(StoreOdreceipt storeodreceipt){

        int res = odreceiptService.insertStoreOdreceipt(storeodreceipt);
        JSONObject resjson = new JSONObject();
        if(res==1){
            resjson.put("storeodreceiptId",storeodreceipt.getId());
        }
        else{
            resjson.put("storeodreceiptId",0);
        }
        return JSON.toJSONString(resjson);
    }

    //前往处置交接单列表页面
    @RequestMapping("/listOdreceiptView")
    public String listOdreceiptView(){
        return "odreceipt/listodreceipt";
    }

    //前往处置交接计划单列表页面
    @RequestMapping("/listStoreOdreceiptView")
    public String listStoreOdreceiptView(){
        return "odreceipt/liststoreodreceipt";
    }

    //前往处置交接单列表页面-管理员
    @RequestMapping("/listOdreceiptView_admin")
    public String listOdreceiptView_admin(){
        return "listodreceipt_admin";
    }

    //处置交接单列表
    @RequestMapping("/listOdreceipt")
    @ResponseBody
    public String listOdreceipt(String startDate, String endDate, String name, String area){
        if(area.equals("全部区域")){
            area="";
        }
        List list = odreceiptService.listOdreceipt(startDate, endDate, name, area);
        JSONObject resjson = new JSONObject();
        if(list != null){
            resjson.put("result",list);
        }
        else{
            resjson.put("result",0);
        }
        return JSON.toJSONString(resjson);
    }

    //处置交接计划单列表
    @RequestMapping("/listStoreOdreceipt")
    @ResponseBody
    public String listStoreOdreceipt(String startDate, String endDate, String name, String area){
        if(area.equals("全部区域")){
            area="";
        }
        List list = odreceiptService.listStoreOdreceipt(startDate, endDate, name, area);
        JSONObject resjson = new JSONObject();
        if(list != null){
            resjson.put("result",list);
        }
        else{
            resjson.put("result",0);
        }
        return JSON.toJSONString(resjson);
    }

    //修改处置交接单
    @RequestMapping("/updateOdreceipt")
    @ResponseBody
    public String updateOdreceipt(String id, String weight)
    {
        if(weight==null){
            weight= String.valueOf(0);
        }
        int result = odreceiptService.updateOdreceipt(id, weight);
        JSONObject resjson = new JSONObject();
        if(result>0){
            resjson.put("result",true);
        }
        else{
            resjson.put("result",false);
        }
        return JSON.toJSONString(resjson);


    }

    //处置单位列表
    @RequestMapping("/listOdunit")
    @ResponseBody
    public String listOdunit(){

        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
//        map.add("year",searchDate.substring(0,4));
        // 组装请求体
        HttpEntity<MultiValueMap<String, String>> request =
                new HttpEntity<MultiValueMap<String, String>>(map, headers);
        String s = restTemplate.postForObject(REST_URL_PREFIX + "/Odreceipt/listOdunit", request,String.class);
        return s;
    }

    //处置单位区域列表
    @RequestMapping("/listOdarea")
    @ResponseBody
    public String listOdarea(){
        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
//        map.add("year",searchDate.substring(0,4));
        // 组装请求体
        HttpEntity<MultiValueMap<String, String>> request =
                new HttpEntity<MultiValueMap<String, String>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX + "/Odreceipt/listOdarea", request,String.class);

    }

    //查询处置单位
    @RequestMapping("/selectOdunit")
    @ResponseBody
    public String selectOdunit(String name){
        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("name",name);
        // 组装请求体
        HttpEntity<MultiValueMap<String, String>> request =
                new HttpEntity<MultiValueMap<String, String>>(map, headers);
        String s = restTemplate.postForObject(REST_URL_PREFIX + "/Odreceipt/selectOdunit", request, String.class);
        return s;

    }

}
